package com.jt.www.queue.listen;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @author admin_ltf
 * @create 2019-04-13 10:34
 */
@Component
public class MessageQueueListner {

    public static final Logger log = LoggerFactory.getLogger(MessageQueueListner.class);
    /*
    @Autowired
    RabbitmqRecordEntityMapper rabbitmqRecordEntityMapper;
    @Autowired
    MessageInfoEntityMapper messageInfoEntityMapper;
    @Autowired
    MessageQueueListnerServiceImpl messageQueueListnerImpl;
// 暂时注掉
//    @RabbitListener(queues = "edu-direct-message")
    public void orderProcess(Message msg, Channel channel) {
        MessageInfoEntity messageInfoEntity = new MessageInfoEntity();
        // 接收到消息
        byte[] msgbody = msg.getBody();// 消息内容
        String content = new String(msgbody);// 把byte[]的消息内容准换为字符串

        // 将消息转化为类  方便消费的时候 做入库处理 message_info
        RabbitMessageInfoVo rabbitMessageInfoVo = null;
        rabbitMessageInfoVo = JsonUtils.fromJson(content, RabbitMessageInfoVo.class);

        //消费消息

        // 消息在消费之前  先记录入库rabbit_record  消费之后 修改某些字段的状态
        if(rabbitMessageInfoVo != null) {
            // 消息在消费之前  需要判断是否消费过(未发送的  未删除  未被消费的)
            RabbitmqRecordEntity rabbitmqRecordEntity = new RabbitmqRecordEntity();
            String messageId = rabbitMessageInfoVo.getMessageId();
            rabbitmqRecordEntity.setMessageId(messageId);
//            rabbitmqRecordEntity.setIsSend(true);// 已发送
//        rabbitmqRecordEntity.setIsDeleted(false);//
            int i = rabbitmqRecordEntityMapper.selectCount(rabbitmqRecordEntity);
            Long id = null;
            // 如果没有查询到数据  说明  该消息是一个新消息或者说没有消费成功 ,做入库处理 。否则是一个重复消息，屏蔽掉即可。
            if (i == 0) {
                rabbitmqRecordEntity = new RabbitmqRecordEntity();
                rabbitmqRecordEntity.setRecordContent(msgbody);
                rabbitmqRecordEntity.setCreateBy("");// 消息的创建人  怎么获取？？？
                rabbitmqRecordEntity.setCreateTime(new Date());// 消息的创建时间
                rabbitmqRecordEntity.setIsDeleted(false);// 消息是否删除:1-是；0-否
                rabbitmqRecordEntity.setIsSend(false);//是否已发送:1-是；0-否
                rabbitmqRecordEntity.setMessageId(rabbitMessageInfoVo.getMessageId());// uuid  消息的唯一性标识 做消费的幂等性处理  怎么获取。消息中有么？？？
                rabbitmqRecordEntity.setRecordType(1);//消息记录类型: 1-个人消息
                rabbitmqRecordEntity.setUpdateTime(new Date());// 消息的修改时间
                rabbitmqRecordEntity.setUpdateBy("");// 消息的修改人   怎么获取？？？
                rabbitmqRecordEntityMapper.insert(rabbitmqRecordEntity);
                if(rabbitmqRecordEntity != null){

                    id = rabbitmqRecordEntity.getId();
                    //具体消费 入库message_info
                    messageQueueListnerImpl.insertMesageInfo(rabbitMessageInfoVo,id,messageId);
                }else{
                    log.error("Messages with ID of " + messageId + "This message consumed fail");
                }
                try {
                    channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
                } catch (IOException e) {
                    log.error("消息回执失败，", e);
                }
            } else {
                log.info("Messages with ID of " + messageId + "This message exists and cannot be consumed repeatedly");
                try {
                    channel.basicAck(msg.getMessageProperties().getDeliveryTag(), false);
                } catch (IOException e) {
                    log.error("消息回执失败，", e);
                }
            }

        }

        }
*/
}
